package twitter.RequestHandler;
import java.io.PrintWriter;

import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import twitter.Database.TwitterMaintainer;
import twitter.RequestParser.BackendRequestParser;
import twitter.TwitterLibrary.Utility;
/*****************************************************
 * This class is designed for handling the back end  request
 * @author fang
 *
 */
public class BackendHttpRequestHandler extends HTTPRequestHandler{
	protected BackendRequestParser parser;
	

	static Logger logger=Logger.getLogger(BackendHttpRequestHandler.class);
	/**************************************************
	 * Class constructor
	 * @param end
	 * @param rwl
	 */
	public BackendHttpRequestHandler(ReentrantReadWriteLock rwl,Utility utility)
	{
		super(rwl,utility);
		BasicConfigurator.configure();
	}
	/********************************************
	 * set the TwitterMaintainer for the BackendRequestParser
	 * @param twitterMaintainer
	 */
	public void setTwitterMaintainer(TwitterMaintainer twitterMaintainer)
	{
		parser=new BackendRequestParser(twitterMaintainer,this.utility);
	}
	/****************************************************
	 * parse the request to BackendRequestParser and write to result to PrintWriter
	 * @param query : the query string
	 * @param out: the PrintWriter output
	 */
	public void parseHttpRequest(String query,PrintWriter out)
	{
		
		query=query.substring(0, query.indexOf("backend"));
		logger.info("processing one request");
		String result=parser.readQueries(query);
		
		out.print(result);
		out.flush();
		
		
	}

}
